<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="Hugo 0.98.0" />

  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="author" content="" />
  <meta property="og:url" content="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E7%BB%93%E6%9E%84%E4%BD%93/" />
  <link rel="canonical" href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E7%BB%93%E6%9E%84%E4%BD%93/" /><link rel="alternate" type="application/atom+xml" href="/index.xml" title="信息管理实验室">

  <script type="application/ld+json">
  {
      "@context" : "http://schema.org",
      "@type" : "BlogPosting",
      "mainEntityOfPage": {
           "@type": "WebPage",
           "@id": "\/"
      },
      "articleSection" : "posts",
      "name" : "入门：结构体",
      "headline" : "入门：结构体",
      "description" : "结构体让复杂数据的描述变得简单。\n案例：描述坐标变换 学习一项知识的最好办法，就是动手实践，下面，让我们参见如下的图片\n假设我们希望完成一次坐标旋转变换，我们就需要采取如下的步骤。\n$$ x = x * cos\\theta - y * sin\\theta $$\n$$ y = x * sin\\theta \u002b y * cos\\theta $$\n变换成代码，就如下所示\n\/* point.c Rotation of points BeginnerC *\/ #include \u0026lt;stdio.h\u0026gt;#include \u0026lt;math.h\u0026gt;#define PI 3.1415926 \/* AngleToRad Calc the Rad Arugument angel The angel want to calc Return Value The corresponding rad *\/ double AngleToRad(double angel) {  return angel * (PI \/ 180); } int main() {  double x = 12, y = 12;  double angle = 36;  double x_result = 0, y_result = 0;  x_result = x * cos(AngleToRad(angle)) - y * sin(AngleToRad(angle));  y_result = x * sin(AngleToRad(angle)) \u002b y * cos(AngleToRad(angle));  printf(\u0026#34;(%lf, %lf) to (%lf, %lf)\\n\u0026#34;, x, y, x_result, y_result);  return 0; } 在这个案例中，我们注意到三件事：",
      "inLanguage" : "en-US",
      "author" : "",
      "creator" : "",
      "publisher": "",
      "accountablePerson" : "",
      "copyrightHolder" : "",
      "copyrightYear" : "2023",
      "datePublished": "2023-01-30 00:00:00 \u002b0000 UTC",
      "dateModified" : "2023-01-30 00:00:00 \u002b0000 UTC",
      "url" : "\/posts\/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97\/%E5%85%A5%E9%97%A8%E7%BB%93%E6%9E%84%E4%BD%93\/",
      "keywords" : [ "C语言入门指南","C语言","信息管理实验室", ]
  }
</script>
<title>入门：结构体</title>
  <meta property="og:title" content="入门：结构体" />
  <meta property="og:type" content="article" />
  <meta property="og:description" content="结构体让复杂数据的描述变得简单。
案例：描述坐标变换 学习一项知识的最好办法，就是动手实践，下面，让我们参见如下的图片
假设我们希望完成一次坐标旋转变换，我们就需要采取如下的步骤。
$$ x = x * cos\theta - y * sin\theta $$
$$ y = x * sin\theta &#43; y * cos\theta $$
变换成代码，就如下所示
/* point.c Rotation of points BeginnerC */ #include &amp;lt;stdio.h&amp;gt;#include &amp;lt;math.h&amp;gt;#define PI 3.1415926 /* AngleToRad Calc the Rad Arugument angel The angel want to calc Return Value The corresponding rad */ double AngleToRad(double angel) {  return angel * (PI / 180); } int main() {  double x = 12, y = 12;  double angle = 36;  double x_result = 0, y_result = 0;  x_result = x * cos(AngleToRad(angle)) - y * sin(AngleToRad(angle));  y_result = x * sin(AngleToRad(angle)) &#43; y * cos(AngleToRad(angle));  printf(&amp;#34;(%lf, %lf) to (%lf, %lf)\n&amp;#34;, x, y, x_result, y_result);  return 0; } 在这个案例中，我们注意到三件事：" />
  <meta name="description" content="结构体让复杂数据的描述变得简单。
案例：描述坐标变换 学习一项知识的最好办法，就是动手实践，下面，让我们参见如下的图片
假设我们希望完成一次坐标旋转变换，我们就需要采取如下的步骤。
$$ x = x * cos\theta - y * sin\theta $$
$$ y = x * sin\theta &#43; y * cos\theta $$
变换成代码，就如下所示
/* point.c Rotation of points BeginnerC */ #include &amp;lt;stdio.h&amp;gt;#include &amp;lt;math.h&amp;gt;#define PI 3.1415926 /* AngleToRad Calc the Rad Arugument angel The angel want to calc Return Value The corresponding rad */ double AngleToRad(double angel) {  return angel * (PI / 180); } int main() {  double x = 12, y = 12;  double angle = 36;  double x_result = 0, y_result = 0;  x_result = x * cos(AngleToRad(angle)) - y * sin(AngleToRad(angle));  y_result = x * sin(AngleToRad(angle)) &#43; y * cos(AngleToRad(angle));  printf(&amp;#34;(%lf, %lf) to (%lf, %lf)\n&amp;#34;, x, y, x_result, y_result);  return 0; } 在这个案例中，我们注意到三件事：" />
  <meta property="og:locale" content="zh-cn" /><meta property="og:image" content="" />
  

  
    <style>body{font-family:bree serif,sans-serif;-webkit-font-smoothing:antialiased;margin:0 20px}article{max-width:800px;margin-left:auto;margin-right:auto}a{color:#000;text-decoration:none}a:hover{font-weight:600;text-decoration:underline}.post-ads{margin:50px 0}.markdown-body{font-size:18px;max-width:100%}.markdown-body a{text-decoration:underline;text-decoration-color:#000}.markdown-body blockquote{margin:0;padding:0 1em;color:#57606a;border-left:.25em solid #d0d7de}.markdown-body pre{padding:16px;overflow:auto;border-radius:10px}.markdown-body code{padding:.2em .4em;font-size:85%;background-color:#f6f8fa;border-radius:6px}.markdown-body pre>code{padding:0;font-size:100%;background-color:inherit;border:0}.Chinese .markdown-body{line-height:200%}.site-date-catalog{font-size:2rem}.header-title{font-size:2rem;font-weight:700;margin-top:32px;font-family:bungee shade,sans-serif}.header-title a{text-decoration:none}.header-subtitle{color:#666}.header-items{margin:10px 0}.header-item{margin:0 5px}.header-line{width:100%;border-width:2px;border-color:#482936;border-style:solid none none none}.lang-switch{font-weight:600}#posts-list{min-height:600px}.posts-line{font-size:1.2rem;margin:12px 0}.posts-categories{font-size:.8rem;margin:auto;text-align:center}.posts-category{padding:3px 0;border:#000 2px solid;border-radius:5px}.site-footer{margin-top:50px}.site-footer-item{margin-right:12px}.post-content img{max-width:100%;display:block;margin-right:auto;margin-top:12px}.post-header{margin-bottom:50px}.post-title{font-size:2rem;font-weight:600}.post-tags{display:inline-block;font-weight:600;padding:2px 5px;margin-right:6px;margin-top:4px;border:#000 2px solid;border-radius:5px}.post-date{font-weight:800;font-style:italic}.post-author{float:right;font-weight:600}.page-content{min-height:60%}.post-content{margin-bottom:50px}.post-content p{hyphens:auto;line-height:1.8;text-justify:ideographic;margin-bottom:1em}.related-content{border-width:3px;border-style:solid;border-color:#000;padding:0 10px;margin-bottom:50px;margin-top:100px}.related-content li{margin:5px 0}.taxonomy-term{font-size:3rem}.gallery-img{text-align:center}.gallery-img span{text-align:center}.gallery-img-desc{font-size:.8em;font-weight:800}@media screen and (max-width:600px){.header-title,.header-subtitle,.header-items{text-align:center}.posts-line{font-size:16px}.markdown-body{font-size:16px}.post-title{font-size:2rem}.post-content p{letter-spacing:.05em}}@media screen and (max-width:48em){.posts-category{display:none}}</style>
  
  
    <style>.container,.container-fluid{margin-right:auto;margin-left:auto}.container-fluid{padding-right:2rem;padding-left:2rem}.row{box-sizing:border-box;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-flex:0;-ms-flex:0 1 auto;flex:initial;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;margin-right:-.5rem;margin-left:-.5rem}.row.reverse{-webkit-box-orient:horizontal;-webkit-box-direction:reverse;-ms-flex-direction:row-reverse;flex-direction:row-reverse}.col.reverse{-webkit-box-orient:vertical;-webkit-box-direction:reverse;-ms-flex-direction:column-reverse;flex-direction:column-reverse}.col-xs,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9,.col-xs-offset-0,.col-xs-offset-1,.col-xs-offset-10,.col-xs-offset-11,.col-xs-offset-12,.col-xs-offset-2,.col-xs-offset-3,.col-xs-offset-4,.col-xs-offset-5,.col-xs-offset-6,.col-xs-offset-7,.col-xs-offset-8,.col-xs-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-xs{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-xs-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-xs-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-xs-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-xs-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-xs-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-xs-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-xs-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-xs-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-xs-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-xs-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-xs-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-xs-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-xs-offset-0{margin-left:0}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-11{margin-left:91.66666667%}.start-xs{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-xs{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-xs{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-xs{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-xs{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-xs{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-xs{-ms-flex-pack:distribute;justify-content:space-around}.between-xs{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-xs{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-xs{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}@media only screen and (min-width:48em){.container{width:49rem}.col-sm,.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-sm-offset-0,.col-sm-offset-1,.col-sm-offset-10,.col-sm-offset-11,.col-sm-offset-12,.col-sm-offset-2,.col-sm-offset-3,.col-sm-offset-4,.col-sm-offset-5,.col-sm-offset-6,.col-sm-offset-7,.col-sm-offset-8,.col-sm-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-sm{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-sm-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-sm-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-sm-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-sm-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-sm-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-sm-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-sm-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-sm-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-sm-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-sm-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-sm-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-sm-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-sm-offset-0{margin-left:0}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-11{margin-left:91.66666667%}.start-sm{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-sm{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-sm{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-sm{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-sm{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-sm{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-sm{-ms-flex-pack:distribute;justify-content:space-around}.between-sm{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-sm{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-sm{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:64em){.container{width:65rem}.col-md,.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-md-offset-0,.col-md-offset-1,.col-md-offset-10,.col-md-offset-11,.col-md-offset-12,.col-md-offset-2,.col-md-offset-3,.col-md-offset-4,.col-md-offset-5,.col-md-offset-6,.col-md-offset-7,.col-md-offset-8,.col-md-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-md{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-md-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-md-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-md-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-md-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-md-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-md-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-md-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-md-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-md-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-md-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-md-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-md-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-md-offset-0{margin-left:0}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-3{margin-left:25%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-6{margin-left:50%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-9{margin-left:75%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-11{margin-left:91.66666667%}.start-md{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-md{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-md{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-md{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-md{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-md{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-md{-ms-flex-pack:distribute;justify-content:space-around}.between-md{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-md{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-md{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:75em){.container{width:76rem}.col-lg,.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-lg-offset-0,.col-lg-offset-1,.col-lg-offset-10,.col-lg-offset-11,.col-lg-offset-12,.col-lg-offset-2,.col-lg-offset-3,.col-lg-offset-4,.col-lg-offset-5,.col-lg-offset-6,.col-lg-offset-7,.col-lg-offset-8,.col-lg-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-lg{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-lg-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-lg-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-lg-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-lg-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-lg-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-lg-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-lg-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-lg-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-lg-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-lg-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-lg-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-lg-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-lg-offset-0{margin-left:0}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-11{margin-left:91.66666667%}.start-lg{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-lg{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-lg{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-lg{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-lg{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-lg{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-lg{-ms-flex-pack:distribute;justify-content:space-around}.between-lg{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-lg{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-lg{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}</style>
  

  

  <link href="/index.xml" rel="alternate" type="application/rss+xml"
    title="信息管理实验室">
  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css?family=Bree+Serif|Bungee+Shade" rel="stylesheet">
  
  

  
  
</head>


<body>
  <article class="post " id="article">
    <div class="row">
      <div class="col-xs-12">
        <div class="site-header">
          
<header>
  <div class="header-title">
    <a href="/"
      >信息管理实验室</a
    >
  </div>
  <div class="header-subtitle"></div>
</header>
<div class="row end-md center-xs header-items">
  
  <div class="header-item">
    <a href="/posts/%e4%bd%a0%e5%a5%bd%e6%9c%8b%e5%8f%8b/" target="_blank">About</a>
  </div>
  
  <div class="header-item">
    <a href="/tags/%e6%88%91%e4%bb%ac%e7%9a%84%e4%bc%99%e4%bc%b4/" target="_blank">Friends</a>
  </div>
  
  <div class="header-item">
    <a href="/tags/%e7%a4%be%e5%8c%ba%e4%b9%a6%e7%b1%8d/" target="_blank">Books</a>
  </div>
  
</div>
<div class="row end-xs">
   
</div>
<div class="header-line"></div>

        </div>
        <header class="post-header">
          <h1 class="post-title">入门：结构体</h1>
          
          <div class="row post-desc">
            <div class="col-xs-6">
              
              <time class="post-date">
                2023-01-30
              </time>
              
              
              <br>
            </div>
          </div>
          <div class="row end-xs">
            
              <a href="/author/%e6%95%ac%e7%ab%a0/"><strong>&nbsp;@敬章</strong></a>        
            
          </div>
          
        </header>

        <div class="post-content markdown-body">
          
          <p>结构体让复杂数据的描述变得简单。</p>
<h2 id="案例描述坐标变换">案例：描述坐标变换</h2>
<p>学习一项知识的最好办法，就是动手实践，下面，让我们参见如下的图片</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673167658906.png" alt="1673167658906"></p>
<p>假设我们希望完成一次坐标旋转变换，我们就需要采取如下的步骤。</p>
<p>$$
x = x * cos\theta - y * sin\theta
$$</p>
<p>$$
y = x * sin\theta + y * cos\theta
$$</p>
<p>变换成代码，就如下所示</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673168911936.png" alt="1673168911936"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    point.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Rotation of points
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;math.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#define PI 3.1415926
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    AngleToRad
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        Calc the Rad
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Arugument
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        angel
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">            The angel want to calc
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Return Value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        The corresponding rad
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">double</span> <span style="color:#a6e22e">AngleToRad</span>(<span style="color:#66d9ef">double</span> angel)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> angel <span style="color:#f92672">*</span> (PI <span style="color:#f92672">/</span> <span style="color:#ae81ff">180</span>);
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> x <span style="color:#f92672">=</span> <span style="color:#ae81ff">12</span>, y <span style="color:#f92672">=</span> <span style="color:#ae81ff">12</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> angle <span style="color:#f92672">=</span> <span style="color:#ae81ff">36</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> x_result <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, y_result <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    x_result <span style="color:#f92672">=</span> x <span style="color:#f92672">*</span> cos(AngleToRad(angle)) <span style="color:#f92672">-</span> y <span style="color:#f92672">*</span> sin(AngleToRad(angle));
</span></span><span style="display:flex;"><span>    y_result <span style="color:#f92672">=</span> x <span style="color:#f92672">*</span> sin(AngleToRad(angle)) <span style="color:#f92672">+</span> y <span style="color:#f92672">*</span> cos(AngleToRad(angle));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;(%lf, %lf) to (%lf, %lf)</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, x, y, x_result, y_result);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这个案例中，我们注意到三件事：</p>
<ul>
<li>我们编写了一个函数</li>
</ul>
<p>在C语言中，自定义函数的基本语法便是</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span>return_value <span style="color:#a6e22e">function_name</span>(argument_list)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">/* Code */</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>而在这个案例中，我们使用 AngleToRad 实现角度到弧度的转换</p>
<ul>
<li>我们定义了一个宏 PI</li>
</ul>
<p>在预处理阶段，代码里面所有的 PI 都会被替换为 3.1415926</p>
<p>宏的意义在于，赋予一个数字以有用的含义</p>
<ul>
<li>在编译的时候，我们添加了一个选项 -lm</li>
</ul>
<p>这一选项的含义，在于引入新的函数库。</p>
<p>在C语言中，所有的一切，都是组件，而各个组件合并在一起，就成为了我们的程序。</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673169916468.png" alt="1673169916468"></p>
<p>事实上，这一错误的含义，就在于，编译器没有找到函数的定义（undefined reference），换而言之，我们知道有（math.h 提供了函数的声明），却不知道函数的实现。</p>
<p>解决的办法，就是引入对应的库（库就是由系统或其它第三方提供的函数实现集合，比如C语言标准库)，让我们的函数，能够有具体的实现。</p>
<p>言归正传，我们可以明确的发现，对于坐标变换而言，这一实现很明显是不恰当的：</p>
<ul>
<li>它让相互关联的x, y相互分离，让数据变得碎片化</li>
</ul>
<p>解决的办法，就是引入结构体，让我们的数据，彼此关联。</p>
<p>声明结构体的基本语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#66d9ef">struct</span> struct_name
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">/* value list */</span>
</span></span><span style="display:flex;"><span>};
</span></span></code></pre></div><p>定义一个结构体变量</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#66d9ef">struct</span> struct_name value_name;
</span></span></code></pre></div><p>使用结构体变量</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span>value.value_list_value;
</span></span></code></pre></div><p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673170365848.png" alt="1673170365848"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    point_second.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Rotation of points
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;math.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#define PI 3.1415926
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">struct</span> point
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> x;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> y;
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    AngleToRad
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        Calc the Rad
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Arugument
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        angel
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">            The angel want to calc
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Return Value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        The corresponding rad
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">double</span> <span style="color:#a6e22e">AngleToRad</span>(<span style="color:#66d9ef">double</span> angel)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> angel <span style="color:#f92672">*</span> (PI <span style="color:#f92672">/</span> <span style="color:#ae81ff">180</span>);
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">struct</span> point point <span style="color:#f92672">=</span> {<span style="color:#ae81ff">12</span>, <span style="color:#ae81ff">12</span>}, point_result <span style="color:#f92672">=</span> {<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> angle <span style="color:#f92672">=</span> <span style="color:#ae81ff">36</span>;
</span></span><span style="display:flex;"><span>    point_result.x <span style="color:#f92672">=</span> point.x <span style="color:#f92672">*</span> cos(AngleToRad(angle)) <span style="color:#f92672">-</span> point.y <span style="color:#f92672">*</span> sin(AngleToRad(angle));
</span></span><span style="display:flex;"><span>    point_result.y <span style="color:#f92672">=</span> point.x <span style="color:#f92672">*</span> sin(AngleToRad(angle)) <span style="color:#f92672">+</span> point.y <span style="color:#f92672">*</span> cos(AngleToRad(angle));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;(%lf, %lf) to (%lf, %lf)</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, point.x, point.y, point_result.x, point_result.y);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这一案例之中，我们用结构体对数据进行了包装，并用结构体的语法使用了变量。</p>
<p>事实上，我们可以将这一问题抽象为函数，让问题得到更大的简化。</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673171309529.png" alt="1673171309529"></p>
<p>很明显地发现，在我们抽象出自己的 PointRoute 函数之后，我们的代码可读性得到了很大的提升，顺利地解决了问题。</p>
<h2 id="案例描述博客文章">案例：描述博客文章</h2>
<p>我们喜欢专注于写作，而将构建网站这些繁琐的工作，交付给工具去做。</p>
<p>正如您所见，我们需要一种描述每一篇文章的方式，用以实现主页的构建。</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673176743342.png" alt="1673176743342"></p>
<p>抛开上面的“社区书籍&quot;这个标题不说，我们可以把问题专注于下面的标签与书籍名称</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673176862918.png" alt="1673176862918"></p>
<p>事实上，它就由 <code>[TAG]-[TITLE]</code> ，两个部分组成。</p>
<p>而想要描述这么一个简单的数据，其实我们就可以设计出一个如下的结构体。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#66d9ef">struct</span> book
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> tag[<span style="color:#ae81ff">256</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> title[<span style="color:#ae81ff">256</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> content[<span style="color:#ae81ff">256</span>];
</span></span><span style="display:flex;"><span>};
</span></span></code></pre></div><p>如您所见，它就有3个变量组成，其中一个描述 标签（tag），一个描述标题（title），还有一个代表文件的内容。</p>
<p>而我们就可以依据这个逻辑，设计一个简单的程序。</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673178019416.png" alt="1673178019416"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    community_book.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Describle some community books
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">struct</span> book
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> tag[<span style="color:#ae81ff">256</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> title[<span style="color:#ae81ff">256</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> content[<span style="color:#ae81ff">256</span>];
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">struct</span> book book_list[<span style="color:#ae81ff">3</span>] <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>        {<span style="color:#e6db74">&#34;C语言&#34;</span>, <span style="color:#e6db74">&#34;C程序设计语言&#34;</span>, <span style="color:#e6db74">&#34;Hello World&#34;</span>}, 
</span></span><span style="display:flex;"><span>        {<span style="color:#e6db74">&#34;C语言&#34;</span>, <span style="color:#e6db74">&#34;C语言入门很简单&#34;</span>, <span style="color:#e6db74">&#34;马磊老师是我的编程启蒙人&#34;</span>}, 
</span></span><span style="display:flex;"><span>        {<span style="color:#e6db74">&#34;伦理学著作&#34;</span>, <span style="color:#e6db74">&#34;道德情操论&#34;</span>, <span style="color:#e6db74">&#34;人最大的缺点是自欺欺人&#34;</span>}
</span></span><span style="display:flex;"><span>        };
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#66d9ef">sizeof</span>(book_list) <span style="color:#f92672">/</span> <span style="color:#66d9ef">sizeof</span>(book_list[<span style="color:#ae81ff">0</span>]);i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;[%s]</span><span style="color:#ae81ff">\n\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\n\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, book_list[i].tag, book_list[i].title, book_list[i].content);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>如您所见，我们构造了一个<strong>结构体数组</strong>，并对他们做出了循环输出。</p>
<p>在实践中，我们往往会从文件进行读入，并以 HTML 代码的形式输出他们。</p>
<h2 id="结构体的本质">结构体的本质</h2>
<p><strong>结构体的本质就是对二进制数据的再描述</strong>。</p>
<p>让我们来看一个案例。</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673185683410.png" alt="1673185683410"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    struct_bit.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use the struct to descible the bit
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">struct</span> char_bit
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_1 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_2 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_3 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_4 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_5 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_6 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_7 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">unsigned</span> bit_8 : <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">struct</span> char_bit bits <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c <span style="color:#f92672">=</span> <span style="color:#ae81ff">27</span>;
</span></span><span style="display:flex;"><span>    bits <span style="color:#f92672">=</span> <span style="color:#f92672">*</span>(<span style="color:#66d9ef">struct</span> char_bit<span style="color:#f92672">*</span>)(<span style="color:#66d9ef">void</span><span style="color:#f92672">*</span>)(<span style="color:#f92672">&amp;</span>c);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d %d %d %d %d %d %d %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, bits.bit_1, bits.bit_2, bits.bit_3, bits.bit_4 \
</span></span><span style="display:flex;"><span>    , bits.bit_5, bits.bit_6, bits.bit_7, bits.bit_8);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这里，我们使用了两种新的方式来处理我们的数据。</p>
<p>第一种叫做位域，在这里，位域就代表了每一个元素所占用的二进制位，我们这里设定为一位，就代表，只能是 0 / 1</p>
<p>第二种叫做指针，<strong>指针的本质是内存地址，类型的本质是数据解析方式</strong>，在这里，它经历的三个步骤</p>
<ul>
<li>第一步，我们用 &amp;c 方式，得到变量c的内存地址（数据储存于内存中）</li>
<li>第二步，我们将这个地址用通用指针进行描述（void* 指针是一个桥梁，代表“单纯的地址&quot;（带类型的地址往往还决定了它的解析方式，比如，<code>char*</code>代表我们处理的是字符数据，会采用字符方式进行对待，而 void* 不是，它就是地址)）</li>
<li>第三步，我们将这个地址（变量c的地址）按照我们设定的内存布局方式进行解析，存入 bits 中</li>
</ul>
<p>最终，我们非常顺利地得到了 27 的二进制位布局</p>
<p><img src="image/%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%8E%E8%81%94%E5%90%88%E4%BD%93/1673186366684.png" alt="1673186366684"></p>
<p>这验证了我们的结论：<strong>C语言的结构体，就是对二进制数据的再描述</strong></p>

        </div>

        <div class="row middle-xs">
          <div class="col-xs-12">
            
            <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/">
                C语言入门指南
              </a>
            </div>
            
            <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80/">
                C语言
              </a>
            </div>
            
            <div class="post-tags">
              <a href="/tags/%E4%BF%A1%E6%81%AF%E7%AE%A1%E7%90%86%E5%AE%9E%E9%AA%8C%E5%AE%A4/">
                信息管理实验室
              </a>
            </div>
            
          </div>
        </div>
        
          <div class="row">
            <div class="col-xs-12">
              
            </div>
          </div>

          

<div class="related-content">
  <h3>Related Posts</h3>
  <ul>
    
    <li><a href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8c%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E9%80%BB%E8%BE%91%E4%B8%8E%E5%BE%AA%E7%8E%AF/">入门：C语言中的逻辑与循环</a></li>
    
    <li><a href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%87%BD%E6%95%B0/">入门：函数</a></li>
    
    <li><a href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%8F%98%E9%87%8F/">入门：变量</a></li>
    
  </ul>

</div>


          
          
          <div style="height: 50px;"></div>
          
        

        <div class="site-footer">
  
  
</div>

      </div>
    </div>
  </article>

  

<script>
  
  
    
    
  
</script>

  

</body>

</html>